module.exports = class extends baseLogic {
    constructor() {
        super();
    }

    static init() {
        return new this();
    }

    async getItems(FindFilterModel, CurPage, PageSize, Sort, curUserInfo) {
        let PlatformRuleItems = {};
        PlatformRuleItems.CurPage = CurPage;
        PlatformRuleItems.PageSize = PageSize;
        let FindFilter = {
            IsDel: 0
        };

        if (FindFilterModel.RuleName) {
            FindFilter.RuleName = {
                '$like': '%' + FindFilterModel.RuleName + '%'
            };
        }



        if (FindFilterModel.CreateTime) {
            FindFilter.CreateTime = {
                '$between': FindFilterModel.CreateTime.split(',')
            };
        }

        if (FindFilterModel.RulePlatformID > 0) {
            FindFilter.RulePlatformID = FindFilterModel.RulePlatformID;
        }


        let data = await this.DB.v_iframe_platform_rule.Access.findAndCountAll({
            attributes: ['RuleID', 'RuleName', 'RuleInfo', 'RuleAble', 'RulePlatformID', 'CreateTime', 'RulePlatformName'],
            where: FindFilter,
            order: [
                Sort.split('|')
            ],
            offset: (CurPage - 1) * PageSize,
            limit: PageSize,
            raw: true
        });
        let DataList = data.rows;
        DataList.map( (item)=> {
            item.CreateTime = this.moment(item.CreateTime).format("YYYY-MM-DD HH:mm:ss");
        });
        PlatformRuleItems.RsCount = data.count;
        PlatformRuleItems.DataList = DataList;
        let PlatformItems = await this.DB.iframe_platform.Access.findAll({
            where: {
                IP_IsDel: 0
            },
            raw: true
        });
        return {
            PlatformRuleItems: PlatformRuleItems,
            PlatformItems: PlatformItems
        };
    }

    async getItem(id) {
        let ret = {};
        ret.Model= {
            RuleAble:[]
        };
        if(id>0){
            ret.Model = await this.DB.iframe_platform_rule.Access.findOne({
                where:{
                    RuleID:id
                },
                raw:true
            });
            if (!ret.Model) {
                return false;
            }
        }
        if(ret.Model && ret.Model.RuleAble==''){
            ret.Model.RuleAble=[];
        }
        
        ret.PlatformItems = await this.DB.iframe_platform.Access.findAll({
            where: {
                IP_IsDel: 0
            },
            raw:true
        });
        ret.SysModules = await this.DB.iframe_system_module.Access.findAll({
            order: [
                ['Sort', 'ASC']
            ],
            raw:true
        });
        return ret ;
    }

    async insertOrUpdate(body){
        var id = body.RuleID ? Number(body.RuleID) : 0;
        var model = {};        
        if (id > 0) {
            model = await this.DB.iframe_platform_rule.Access.findById(id);
            if (!model) {
                return null;
            }
            model = model.dataValues;
        } else {
            model.RuleID=0;
            model.CreateTime=this.moment().format("YYYY-MM-DD HH:mm:ss");
        }
        model.RuleName =body.RuleName;
        model.RuleInfo = body.RuleInfo;
        model.RuleAble = body.RuleAble;
        model.RulePlatformID=body.RulePlatformID;
        model.RuleDict=body.RuleDict;
    
        if (id > 0) {
            model= await this.DB.iframe_platform_rule.Access.update(model, {
                where: {
                    RuleID: id
                }
            });
        } else {
            model= await this.DB.iframe_platform_rule.Access.create(model);
        }
        return model;
    }

    async del(keys){
        return  await this.DB.iframe_platform_rule.Access.update({ IsDel: 1 }, {
            where: {
                RuleID: keys
            }
        });
    }

    async getItemByPlatformId(id){
        return await this.DB.iframe_platform_rule.Access.findAll({
            attributes:['RuleID','RuleName'],
            where:{
                IsDel:0,
                RulePlatformID:id
            },
            raw:true
        });
    }
}